package com.example.test_demo.hashmapVScurrentmap;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 比较hashmap和currenthashmap，作为全局变量哪个是线程安全的？
 * @author Hasee
 * 失败的案例
 */
public class Test {
	public static void main(String[] args) {
		int i =10;
		Map<Integer,Integer> map = new HashMap<Integer, Integer>();
		map.put(0, 0);

		Map<Integer,Integer> cmap = new ConcurrentHashMap<Integer, Integer>();
		cmap.put(0, 0);
		PutValueRunnable runnable = new PutValueRunnable(cmap,i);

		Thread t = new Thread(runnable);
		Thread t2 = new Thread(runnable);
		t.start();
		t2.start();
//		ExecutorService pool = Executors.newFixedThreadPool(10);
//		pool.execute( runnable);
//		pool.execute( runnable);
		System.out.println("map:"+map.get(0));
		System.out.println("i:"+runnable.getI());
		System.out.println("cmap:"+cmap.get(0));

	}

}
